package com.srcpoint.common.tips
{
	
	import com.srcpoint.common.tips.core.ITip;
	import com.srcpoint.common.tips.core.TipPool;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.*;
	import com.srcpoint.common.tips.core.TipItemStyle;
	
	public class TipManager
	{
		private var _stage:Stage;
		private var _parent:DisplayObjectContainer;
		private var _popTipsParent:DisplayObjectContainer;
		private var _toolTipList:Object;
		protected var _mouseOverToolTipList:Dictionary;
		private var _currentTooltip:ITip;
		public static var instance:TipManager;
		public static function getInstance():TipManager{
			if(instance==null){
				instance =new TipManager();
			}
			return instance;
		}
		
		public function TipManager()
		{
			this._toolTipList = {};
			this._mouseOverToolTipList = new Dictionary();
			return;
		}
		
		public function get parent() : DisplayObjectContainer
		{
			if (_parent)
			{
				return _parent;
			}
			return this._stage;
		}
		
		public function set parent(p:DisplayObjectContainer) : void
		{
			this._parent = p;
		}
		
		public function get popTipsParent() : DisplayObjectContainer
		{
			return _popTipsParent;
		}
		
		public function set popTipsParent(p:DisplayObjectContainer) : void
		{
			_popTipsParent = p;
		}
		
//		public function mouseOver(itc:ITipComponent) : void
//		{
//			if (itc)
//			{
//				if (itc.toolTip != "" || itc.toolTipData != null)
//				{
//					show(itc, itc.toolTip, itc.toolTipDirection, itc.toolTipData, itc.toolTipType);
//				}
//			}
//		}
//		
//		public function mouseOut(itc:ITipComponent) : void
//		{
//			if (itc)
//			{
//				hide(itc);
//			}
//		}
		
		public function show(owner:DisplayObjectContainer,text:XML, tipCls:Class,direction:int = 3, view:Object = null) : void
		{
			var itip:ITip = TipPool.getToolTip(tipCls);
			_currentTooltip = itip;
			_mouseOverToolTipList[owner] = itip;
			if (popTipsParent != null)
			{
				popTipsParent.addChild(itip as DisplayObject);
			}
			else
			{
				parent.addChild(itip as DisplayObject);
			}
			itip.show(text, direction, view);
		}
		
		public function hide(owner:DisplayObjectContainer) : void
		{
			if (_mouseOverToolTipList[owner] != null)
			{
				(_mouseOverToolTipList[owner] as ITip).hide();
				delete this._mouseOverToolTipList[owner];
			}
			return;
		}
		
		public function get currentTooltip() : ITip
		{
			return this._currentTooltip;
		}
		
		private var TIP_ITEM_STYLES:Object={};
		public function getTipItemStyle(name:String=''):TipItemStyle{
			return TIP_ITEM_STYLES["TIS_"+name];
		}
		
		public function initTipItemStyle(xml:XML):void{
			TIP_ITEM_STYLES["TIS_"] = new TipItemStyle();
			TIP_ITEM_STYLES["TIS_null"] = TIP_ITEM_STYLES["TIS_"];
			var istyle:TipItemStyle 
			for each(var cfg:XML in xml.children()){
//				for each(var attr:XML in cfg.children()){
//					if(istyle.hasOwnProperty(attr["@name"])){
//						istyle[attr['@name']]=int(attr['@value']);
//					}	
//				}
				istyle = new TipItemStyle();
				istyle.bgColor = cfg.hasOwnProperty('@bgcolor')?int(cfg['@bgcolor']):istyle.bgColor;
				istyle.border = cfg.hasOwnProperty('@border')?int(cfg['@border']):istyle.border;
				istyle.corner = cfg.hasOwnProperty('@corner')?int(cfg['@corner']):istyle.corner;
				istyle.hasBg = cfg.hasOwnProperty('@hasbg')?cfg['@hasbg']=='true':istyle.hasBg;
				istyle.alpha = cfg.hasOwnProperty('@alpha')?Number(cfg['@alpha']):istyle.alpha;
				TIP_ITEM_STYLES["TIS_"+cfg['@name']] = istyle;
			}
		}
	}
}